package com.bigfish.crm.service;

import java.util.List;

import javax.annotation.PostConstruct;

import org.drools.core.io.impl.ByteArrayResource;
import org.kie.api.io.Resource;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.utils.KieHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bigfish.crm.dto.LevelRule;

@Service
public class MyKieService {
	@Autowired
	private LevelRuleService levelRuleService;

	// @KSession("member_ksession")
	//private KieSession kieSession;
	private List<LevelRule> ruleList; 
	//kiebuilder 不能够保存， 相同的规则加进来不会覆盖
	public KieSession getKieSession() {
		KieHelper kh = new KieHelper();
		for (int i = 0; i < ruleList.size(); i++) {
			Resource resource = new ByteArrayResource(ruleList.get(i).getContent().getBytes());
			kh.addResource(resource, ResourceType.DRL);
		}
		// kh.addContent(sb.toString(), ResourceType.DRL);
		KieSession kss = kh.build().newKieSession();
		return kss;
	}
	@PostConstruct
	public void init(){
		ruleList=levelRuleService.ableRuleList();
	}
	public void refreshKieSession(){
		ruleList=levelRuleService.ableRuleList();
	}
}
